package com.etc.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.dao.CommentsMapper;
import com.etc.entity.Comments;
import com.etc.service.CommentsService;

@Service
public class CommentsServiceImpl implements CommentsService {

	@Autowired
	private CommentsMapper commentsMapper;

	@Override
	public List<List<Comments>> findAllCommentsByCid(Integer aid) {
		// 这里的参数0代表的是根节点
		List<Comments> rootCommentsCid = commentsMapper.selectRootCidBycarticleid(aid);
		// 存放所有评论的list集合，list中放list
		List<List<Comments>> commentsList = new ArrayList<List<Comments>>();
		for (Comments comment : rootCommentsCid) {
			List<Comments> rootNumList = commentsMapper.selectCommentByRootNum(comment.getCid());
			// 将同一个树放入数组中
			commentsList.add(rootNumList);
		}
		Collections.reverse(commentsList);
		return commentsList;
	}

	@Override
	public int countArticleComments(Integer carticleid) {
		
		return commentsMapper.countArticleComments(carticleid);
	}

	@Override
	public int insertComments(Comments comment) {
		
		return commentsMapper.insertComments(comment);
	}

	@Override
	public int updateCommentBycid(Comments comment) {
		return commentsMapper.updateCommentBycid(comment);
	}

	@Override
	public Boolean deleteCommentBycid(Integer cid) {
		
		int i = commentsMapper.deleteByPrimaryKey(cid);
		if (i>0) {
			return true;
		}else{
			return false;
		}
		
	}

}
